users = function() {
	//定义搜索窗体
	var USER_SEARCH_WIN = null;
	//定义添加修改窗体
	var USER_ADDOREDIT_WIN = null;
	//定义权限设置窗体
	var USERCONFIGWIN = null;
	//定义状态STORE
	var USERSTATESTORE = new Store({
		fields: ['id', 'str'],
		data: [
		   { id: true, str: '激活' },
		   { id: false, str: '失效' }
		   
		]
	});
	return {
		/*--------------------------------------窗体定义--------------------------------------------*/
		//定义GRID窗体
		panel: function() {
			var store = new Store({
				fields: ['id', 'username', 'truename', 'enabled', 'loginTime', 'loginIp', 'password'],
				proxy: {
			        type: 'ajax',
			        url: '../admin/user.do?index',
			        reader: {
			            type: 'json',
			            root: 'datas',
			            totalProperty: 'total'
			        }
			    }
			});
			
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				username: '',
				truename: ''
			};
			store.load();
			
			var pageToolBar = new PagingToolbar({
	            store: store,
	            draggable : false,
	            displayInfo: true
	        });
			
			var grid = new Grid({
				title: '用户列表',
				id: 'usergrid',
				region: 'center',
				store: store,
				minColumnWidth: 35,
				stripeRows: true,
				bbar : pageToolBar,
				tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						store.load();
					}
				}, "-", {
					text: '搜索',
					scale: 'medium',
					iconCls: 'icon-search',
					handler: function() {
						users.searchAction();
					}
				}, "-", {
					id: 'userAdd',
					text: '添加',
					scale: 'medium',
					iconCls: 'icon-add',
					disabled: true,
					handler: function() {
						users.addAction();
					}
				}, "-", {
					id: 'userEdit',
					text: '修改',
					scale: 'medium',
					iconCls: 'icon-edit',
					disabled: true,
					handler: function() {
						users.editAction();
					}
				}, "-", {
					id: 'userRemove',
					text: '删除',
					scale: 'medium',
					iconCls: 'icon-remove',
					disabled: true,
					handler: function() {
						users.removeAction();
					}
				}, "-", {
					id: 'userConfig',
					text: '权限设置',
					scale: 'medium',
					iconCls: 'icon-config',
					disabled: true,
					handler: function() {
						users.configAction();
					}
				}],
				columns: [
		             new Ext.grid.RowNumberer(),
		             {header: '序号', dataIndex: 'id', hidden: true},
		             {header: '用户账号', dataIndex: 'username', width: 200},
		             {header: '登录密码', dataIndex: 'password', width: 200},
		             {header: '真实姓名', dataIndex: 'truename', width: 200},
		             {header: '状态', dataIndex: 'enabled', width: 100, renderer: function(v) {
		            	 return USERSTATESTORE.getById(v).get('str'); 
		             }},
		             {header: '上次登录IP', dataIndex: 'loginIp', width: 150},
		             {header: '上次登录时间', dataIndex: 'loginTime', width: 150}
				]
			});
			
			var panel = new Panel({
				border: false,
				layout: 'border',
				items: [grid]
			});
			
			return panel;
		},
		//定义搜索窗体
		searchWin: function() {
			if (USER_SEARCH_WIN == null) {
				USER_SEARCH_WIN = new Window({
					id: 'userSearchWin',
				    title: '搜索用户',
				    height: 200,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'username',
				        	fieldLabel: '用户账号'
				        }, {
				        	name: 'truename',
				        	fieldLabel: '真实姓名'
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		users.searchSureAction(fom);
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		USER_SEARCH_WIN.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			USER_SEARCH_WIN.show();
		},
		
		//定义添加修改窗体
		addOrEditWin: function() {
			if (USER_ADDOREDIT_WIN == null) {
				USER_ADDOREDIT_WIN = new Window({
					id: 'userAddOrEditWin',
				    title: '编辑用户信息',
				    height: 230,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	id: 'userAddOrEditForm',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'id',
				        	hidden: true
				        }, {
				        	fieldLabel: '用户账号',
				        	name: 'username',
				        	allowBlank: false
				        }, {
				        	fieldLabel: '真实姓名',
				        	name: 'truename',
				        	allowBlank: false
				        }, {
				        	fieldLabel: '登录密码',
				        	name: 'password',
				        	allowBlank: false
				        }, {
				        	fieldLabel: '状态',
				        	xtype: 'combo',
			             	name: 'enabled',
			             	store: USERSTATESTORE,
			                displayField: 'str',
			                valueField: 'id',
			                queryMode: 'local',
			                editable: false
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		if (fom.isValid()) {
				        			users.sumbitAction(fom);
				        		}
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		USER_ADDOREDIT_WIN.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			USER_ADDOREDIT_WIN.show();
		},
		
		//权限设置
		configWin: function() {
			if (USERCONFIGWIN == null) {
				var store = new Store({
					fields: ['id', 'name', 'remark'],
					proxy: {
				        type: 'ajax',
				        url: '../admin/user.do?configtorole'
				    },
				    autoLoad: true,
				    listeners: {
				    	load: function() {
				    		users.initAction();
				    	}
				    }
				});
				
				var sm = Ext.create('Ext.selection.CheckboxModel', {
					mode: 'SINGLE'
				});
				
				USERCONFIGWIN = new Window({
					id: 'userConfigWin',
				    title: '角色设置',
				    height: 600,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'gridpanel',
				    	id: 'userrolegrid',
				    	store: store,
						minColumnWidth: 35,
						stripeRows: true,
						selModel: sm,
						columns: [
						    {header: '序号', dataIndex: 'id', hidden: true},
			                {header: '角色名称', dataIndex: 'name', width: 200},
			                {header: '备注说明', dataIndex: 'remark', width: 200}
			            ]
				    }],
				    buttons: [{
				    	text: '确定',
				    	handler: function() {
				    		users.configSaveAction();
				    	}
				    }, {
				    	text: '取消',
				    	handler: function() {
				    		USERCONFIGWIN.hide();
				    	}
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		Ext.getCmp('userrolegrid').getSelectionModel().deselectAll();
			        	}
			        }
				});
			} else {
				users.initAction();
			}
			USERCONFIGWIN.show();
		},
		
		/*--------------------------------------事件定义--------------------------------------------*/
		//搜索窗体事件
		searchAction: function() {
			users.searchWin();
		},
		
		//搜索确定事件
		searchSureAction: function(fom) {
			var store = Ext.getCmp('usergrid').getStore();
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				username: fom.findField('username').value,
				truename: fom.findField('truename').value
			};
			store.load();
			USER_SEARCH_WIN.hide();
		},
		
		//添加窗体事件
		addAction: function() {
			users.addOrEditWin();
		},
		
		//修改窗体事件
		editAction: function() {
			var sel = Ext.getCmp('usergrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要修改的用户！');
			} else {
				users.addOrEditWin();
				Ext.getCmp('userAddOrEditForm').getForm().loadRecord(sel[0]);
			}
		},
		
		//提交确认事件
		sumbitAction: function(fom) {
			var url = '../admin/user.do?edit';
			var id = fom.findField('id').value;
			if (id == '') {
				url = '../admin/user.do?add';
			}
			FormSubmit(fom, url, {}, function(obj) {
				Ext.getCmp('usergrid').getStore().load();
				USER_ADDOREDIT_WIN.hide();
			});
		},
		
		//删除权限事件
		removeAction: function() {
			var sel = Ext.getCmp('usergrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要删除的用户！');
			} else {
				Ext.MessageBox.confirm('确认', '您确认要删除该用户吗？', function(btn) {
					if (btn == "yes") {
						var res = RequestByUrl('../admin/user.do?remove', {
							id: sel[0].get('id')
						});
						Ext.getCmp('usergrid').getStore().load();
						Ext.Show.msg(res.title, '');
					}
				});
			}
		},
		
		//权限设置窗体
		configAction: function() {
			var sel = Ext.getCmp('usergrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要权限设置的用户！');
				return false;
			} else {
				users.configWin();
			}
		},
		
		//初始化用户权限
		initAction: function() {
			var sel = Ext.getCmp('usergrid').getSelectionModel().getSelection();
			var res = RequestByUrl('../admin/user.do?configtouser', {
				id: sel[0].get('id')
			});
			var store = Ext.getCmp('userrolegrid').getStore();
			if (res.title != '') {
				var node = store.getById(parseInt(res.title));
				Ext.getCmp('userrolegrid').getSelectionModel().select(node);
			}
		},
		
		//权限保存事件
		configSaveAction: function() {
			var userSel = Ext.getCmp('usergrid').getSelectionModel().getSelection();
			var roleSel = Ext.getCmp('userrolegrid').getSelectionModel().getSelection();
			var res = RequestByUrl('../admin/user.do?config', {
				id: userSel[0].get('id'),
				roleId: roleSel[0].get('id')
			});
			Ext.Show.msg(res.title, '');
			USERCONFIGWIN.hide();
		}
	};
}();